﻿@model OT.Areas.Admin.Models.Citys
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
        <h3 id="myModalLabel">Edit City</h3>
    </div>
    @Html.HiddenFor(model => model.CityID, new { id = "HDFID" })
    @Html.HiddenFor(model => model.StateID, new { id = "HDFStateID" })
    <div class="modal-body">
        <div id="alert_modal"></div>
        <div class="form-horizontal" role="form">
            <div class="form-group">
                <div class="col-lg-13">
                    <div id="divMsgEdit"></div>
                </div>
            </div>
            <div class="form-group" id="divCountry">
                <label class="col-lg-3 control-label">Country</label>
                <div class="col-lg-5">
                    @Html.DropDownListFor(model => model.CountryID, new SelectList(Model.Country, "CountryID", "CountryName"), "----Select Country---", new { id = "ddlCountry", @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.CountryID)
                </div>
            </div>
            <div class="form-group" id="divState">
                <label class="col-lg-3 control-label">State</label>
                <div class="col-lg-5">
                    @Html.DropDownListFor(model => model.StateID, new SelectList(Model.States, "StateID", "StateName"), "---Select State----", new { id = "ddlState", @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.StateID)
                </div>
            </div>
            <div class="form-group" id="divCityCode">
                <label class="col-lg-3 control-label">City Code</label>
                <div class="col-lg-5">
                    @Html.TextBoxFor(model => model.CityCode, new { @maxlength = "10", @class = "form-control", @placeholder = "Enter City Code" })
                    @Html.ValidationMessageFor(model => model.CityCode)
                </div>
            </div>
            <div class="form-group" id="divCityName">
                <label class="col-lg-3 control-label">City Name</label>
                <div class="col-lg-5">
                    @Html.TextBoxFor(model => model.CityName, new { @maxlength = "50", @class = "form-control", @placeholder = "Enter City Name" })
                    @Html.ValidationMessageFor(model => model.CityName)
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button class="btn btn-default" type="submit" id="btnEdit"><i class="icon-cog"></i>Save</button>
        <button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i>Cancel</button>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        $("#ddlCountry").change(function () {
            var CountryID = $(this).val();
            if (CountryID > 0) {
                $.getJSON("/admin/City/LoadState", { CountryID: CountryID },
                       function (classesData) {
                           var select = $("#ddlState");
                           select.empty();
                           select.append($('<option/>', {
                               value: 0,
                               text: "--Select State--"
                           }));
                           $.each(classesData, function (index, itemData) {

                               select.append($('<option/>', {
                                   value: itemData.Value,
                                   text: itemData.Text
                               }));
                           });
                       });
            }
            else {
                var select = $("#ddlState");
                select.empty();
                select.append($('<option/>', {
                    value: 0,
                    text: "--Select State--"
                }));
            }
        });
    });
    $("#btnEdit").click(function (e) {
        var CityID = $("#HDFID").val();
        var StateID = $("#ddlState option:selected").val();
        var CityName = $("#CityName").val();
        var CityCode = $("#CityCode").val();
        var country = $("#ddlCountry").val();
        e.preventDefault();
        if (CityName == '' || CityCode == '' || StateID == 0 || country == 0) {
            showMsg("divMsgEdit", "Enter all Required Field Value", "danger");
            (CityName == '') ? $('#divCityName').addClass('has-error') : $('#divCityName').removeClass('has-error');
            (CityCode == '') ? $('#divCityCode').addClass('has-error') : $('#divCityCode').removeClass('has-error');
            (StateID == 0) ? $('#divState').addClass('has-error') : $('#divState').removeClass('has-error');
            (country == 0) ? $('#divCountry').addClass('has-error') : $('#divCountry').removeClass('has-error');
        }
        else {
            $.ajax({
                url: "/Admin/City/Edit?CityID=" + CityID + "&CityCode=" + CityCode + "&CityName=" + CityName + "&StateID=" + StateID,
                type: "POST",
                success: function (result) {
                    if (result != "NotValid") {
                        $('#modalDiv').modal('hide');
                        showMsg('divMsgIndex', 'Record Updated Successfuly', 'success');
                        loadPage(1, 10, '');
                    }
                    else {
                        showMsg('divMsgEdit', 'Faild to Update Record', 'warning');
                    }
                },
                error: function (data) {
                }
            });
        }
        return false;
    });
</script>
